package com.jsshse.controller.sys;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

 
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.jsshse.service.sys.ParamGroupServiceI;
import com.jsshse.util.Pages;


@Controller
@RequestMapping("/sys/paramGroup/*")
public class ParamGroupController {
	

	@Resource
	private ParamGroupServiceI paramGroupService;
	
	private static Logger log = Logger.getLogger(ParamGroupController.class);
	
	@RequestMapping(value = "/save")
	public ModelAndView save(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String data = request.getParameter("data");
		log.debug(data);
		JSONObject json = JSONObject.fromObject(data);
		HashMap m= (HashMap)JSONObject.toBean(json,HashMap.class);

		boolean flag=true;
		
		String id=m.get("id").toString();
		log.debug(id);
		if(id.length()>0){
			flag=paramGroupService.update(m);
		}else{
			flag=paramGroupService.add(m);
		}

		m.clear();
		m.put("r", flag);
		
		JSONObject jsonObject = JSONObject.fromObject(m);      
		response.getWriter().write(jsonObject.toString());
		return null;
	}
//	
//	@RequestMapping("/del")
//	public ModelAndView romove (HttpServletRequest request, HttpServletResponse response) throws Exception {
//		 String idStr = request.getParameter("id")==null?"":request.getParameter("id"); 
//		 log.debug("id:"+idStr); 
//		 if(idStr.length()>0){
//			 String[] ids = idStr.split(",");
//			    for (int i = 0, l = ids.length; i < l; i++)
//			    {
//			        paramGroupService.delete(ids[i]);
//			    }  
//		 } 
//		return null;
//	}
//	
	@RequestMapping(value = "/get", method = RequestMethod.POST)
	public ModelAndView get(@RequestParam("id") String id,HttpServletRequest request, HttpServletResponse response) throws Exception {
		Map map = paramGroupService.queryById(id);
		JSONObject jsonObject = JSONObject.fromObject(map); 
		log.debug(jsonObject.toString()); 
	    response.getWriter().write(jsonObject.toString());
		return null;
	}
	
	@RequestMapping("/list")
	public ModelAndView listAll (HttpServletRequest request, HttpServletResponse response) throws Exception {
		 String key = request.getParameter("key")==null?"":request.getParameter("key");
		 int pageIndex = Integer.parseInt(request.getParameter("page"));
			int pageSize = Integer.parseInt(request.getParameter("rows"));        
			String sortField = request.getParameter("sort");
			String sortOrder = request.getParameter("order");

	    StringBuffer sb=new StringBuffer();
	    Pages pages=new Pages();
	    
	    if(key.length()>0){
	    	sb.append(" AND TAG LIKE '%"+key+"%'");
	    }
    
	    pages.setSortField(sortField);
	    pages.setSortOrder(sortOrder);
	    pages.setQueryCondition(sb.toString());
	    pages.setPageSize(pageSize);
	    pages.setPageStart((pageIndex-1)*pageSize);
	    
	    pages = paramGroupService.queryForList(pages);
	    
	    HashMap result=new HashMap();
	    result.put("rows",pages.getDataList());
        result.put("total",pages.getTotal());
       
        JSONObject jsonObject = JSONObject.fromObject(result);      
        response.getWriter().write(jsonObject.toString());
		return null;
	}
	@RequestMapping(value = "/getParam")
	public ModelAndView getParam(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String[] info=request.getParameterValues("info");
		if(info[0].length()!=0){
			log.debug(info);
			List list= paramGroupService.queryParamByTag(info);
			JSONArray json = JSONArray.fromObject(list); 
			log.debug(json.toString()); 
		    response.getWriter().write(json.toString());
		}
		return null;
	}
	@RequestMapping(value = "/getParamGrid")
	public ModelAndView getParamGrid(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String[] info=request.getParameterValues("info");
		if(info[0].length()!=0){
			log.debug(info);
			List list= paramGroupService.queryParamByTag(info);
			HashMap m= new HashMap();
			m.put("total", list.size());
			m.put("rows", list);
			JSONObject json = JSONObject.fromObject(m); 
			log.debug(json.toString()); 
			response.getWriter().write(json.toString());
		}
		return null;
	}
	 
}
